<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="样品编号" prop="sampleNumber">
        <template>
          <el-select v-model="dataForm.sampleNumber" :disabled="dataForm.id != 0"  placeholder="请选择" @change="getProductInfo()">
            <el-option
                v-for="item in option"
                :key="item.prProjectNumber"
                :label="item.prProjectName"
                :value="item.prProjectNumber">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="样品名称" prop="prototypeTrademark">
        <el-input v-model="dataForm.prototypeTrademark" placeholder="样品名称"></el-input>
      </el-form-item>
      <el-form-item label="开始检测日期" prop="startingTime1">
        <div class="block">
          <el-date-picker
              v-model="dataForm.startingTime1"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期时间">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="结束检测日期" prop="endTime1">
        <div class="block">
          <el-date-picker
              v-model="dataForm.endTime1"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期时间">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="检测地点" prop="detectionSite">
        <template>
          <el-select v-model="dataForm.detectionSite" placeholder="请选择">
            <el-option
                v-for="item in eoptions"
                :key="item.id"
                :label="item.name"
                :value="item.name">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="检测人" prop="inspector">
        <template>
          <el-select v-model="dataForm.inspector" placeholder="请选择">
            <el-option
                v-for="item in detection"
                :key="item.adId"
                :label="item.adName"
                :value="item.adName">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="领样地点" prop="sampleCollectionSite">
        <template>
          <el-select v-model="dataForm.sampleCollectionSite" placeholder="请选择">
            <el-option
                v-for="item in eoptions"
                :key="item.id"
                :label="item.name"
                :value="item.name">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="领样人" prop="sampleCollectionPerson">
        <template>
          <el-select v-model="dataForm.sampleCollectionPerson" placeholder="请选择">
            <el-option
                v-for="item in detection"
                :key="item.adId"
                :label="item.adName"
                :value="item.adName">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="还样地点" prop="sampleReturnSite">
        <template>
          <el-select v-model="dataForm.sampleReturnSite" placeholder="请选择">
            <el-option
                v-for="item in eoptions"
                :key="item.id"
                :label="item.name"
                :value="item.name">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="还样人" prop="sampleReturnPerson">
        <template>
          <el-select v-model="dataForm.sampleReturnPerson" placeholder="请选择">
            <el-option
                v-for="item in detection"
                :key="item.adId"
                :label="item.adName"
                :value="item.adName">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="领样状态" prop="sampleLeading">
        <el-radio class="sampleLeading" v-model="dataForm.sampleLeading" label="0">样品完好</el-radio>
        <el-radio class="sampleLeading" v-model="dataForm.sampleLeading" label="1">样品异常</el-radio>
      </el-form-item>
      <el-form-item label="样品检测状态" prop="sampleTesting">
        <el-radio class="sampleTesting" v-model="dataForm.sampleTesting" label="0">检测正常</el-radio>
        <el-radio class="sampleTesting" v-model="dataForm.sampleTesting" label="1">检测异常</el-radio>
        <el-radio class="sampleTesting" v-model="dataForm.sampleTesting" label="2">检测终止</el-radio>
      </el-form-item>
      <el-form-item label="还样状态" prop="sampleReturnStatus">
        <el-radio class="sampleReturnStatus" v-model="dataForm.sampleReturnStatus" label="0">样品完好</el-radio>
        <el-radio class="sampleReturnStatus" v-model="dataForm.sampleReturnStatus" label="1">样品异常</el-radio>
      </el-form-item>

      <el-form-item label="检测开始时间" prop="startingTime">
        <div class="block">
          <el-date-picker
              v-model="dataForm.startingTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期时间">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="检测结束时间" prop="endTime">
        <div class="block">
          <el-date-picker
              v-model="dataForm.endTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期时间">
          </el-date-picker>
        </div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      visible: false,
      option: [],
      eoptions: [],
      detection: [],
      dataForm: {
        id: 0,
        sampleNumber: '',
        prototypeTrademark: '',
        detectionSite: '',
        inspector: '',
        sampleCollectionSite: '',
        sampleCollectionPerson: '',
        sampleReturnSite: '',
        sampleReturnPerson: '',
        sampleLeading: '',
        sampleAnomalyDescription: '',
        sampleReturnStatus: '',
        sampleReturnAnomalyDescription: '',
        sampleSupplier: '',
        returnSample: '',
        returnTime: '',
        sampleTesting:'',
        startingTime:'',
        endTime:'',
        startingTime1:'',
        endTime1:'',

      },
      dataRule: {
        sampleNumber: [
          {required: true, message: '样品编号不能为空', trigger: 'blur'}
        ],
        prototypeTrademark: [
          {required: true, message: '样品名称不能为空', trigger: 'blur'}
        ],
        detectionSite: [
          {required: true, message: '检测地点不能为空', trigger: 'blur'}
        ],
        inspector: [
          {required: true, message: '检测人不能为空', trigger: 'blur'}
        ],
        sampleCollectionSite: [
          {required: true, message: '领样地点不能为空', trigger: 'blur'}
        ],
        sampleCollectionPerson: [
          {required: true, message: '领样人不能为空', trigger: 'blur'}
        ],
        sampleReturnSite: [
          {required: true, message: '还样地点不能为空', trigger: 'blur'}
        ],
        sampleReturnPerson: [
          {required: true, message: '还样人不能为空', trigger: 'blur'}
        ],
        sampleLeading: [
          {required: true, message: '领样状态（样品完好、样品异常）不能为空', trigger: 'blur'}
        ],
        sampleAnomalyDescription: [
          {required: true, message: '样品异常描述不能为空', trigger: 'blur'}
        ],
        sampleReturnStatus: [
          {required: true, message: '还样状态（样品完好、样品异常）不能为空', trigger: 'blur'}
        ],
        sampleReturnAnomalyDescription: [
          {required: true, message: '还样样品异常描述不能为空', trigger: 'blur'}
        ],
        sampleSupplier: [
          {required: true, message: '供样单位负责人不能为空', trigger: 'blur'}
        ],
        returnSample: [
          {required: true, message: '还样人不能为空', trigger: 'blur'}
        ],
        returnTime: [
          {required: true, message: '还样时间不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  mounted() {
    this.getProductNumber()
    this.geteoptions()
    this.getByIdAndName()
  },
  methods: {
    getByIdAndName() {
      this.$http({
        url: this.$http.adornUrl(`/reportApproval/admin/getByIdAndName`),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          this.detection = data.adminList
          console.log(data.product)
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    geteoptions() {
      this.$http({
        url: this.$http.adornUrl('/reportapproval/entrepreneur/geteoptions'),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          this.eoptions = data.entrepreneurList
          console.log(data.entrepreneurList)
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    getProductNumber() {
      this.$http({
        url: this.$http.adornUrl('/reportapproval/product/getProjectNumber'),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          this.option = data.productList
          // console.log(this.options)
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    getProductInfo() {
      this.$http({
        url: this.$http.adornUrl(`/reportapproval/product/getProductInfo/${this.dataForm.sampleNumber}`),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          this.dataForm.prototypeTrademark = data.productInfo.prProducerModel
          this.dataForm.model = data.productInfo.prProducerModel
          // console.log(this.dataForm.productName)
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/reportapproval/sampletransfer/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then((data) => {
            if (data && data.code === 0) {
              this.dataForm.sampleNumber = data.sampleTransfer.sampleNumber
              this.dataForm.prototypeTrademark = data.sampleTransfer.prototypeTrademark
              this.dataForm.detectionSite = data.sampleTransfer.detectionSite
              this.dataForm.inspector = data.sampleTransfer.inspector
              this.dataForm.sampleCollectionSite = data.sampleTransfer.sampleCollectionSite
              this.dataForm.sampleCollectionPerson = data.sampleTransfer.sampleCollectionPerson
              this.dataForm.sampleReturnSite = data.sampleTransfer.sampleReturnSite
              this.dataForm.sampleReturnPerson = data.sampleTransfer.sampleReturnPerson
              this.dataForm.sampleLeading = data.sampleTransfer.sampleLeading
              this.dataForm.sampleAnomalyDescription = data.sampleTransfer.sampleAnomalyDescription
              this.dataForm.sampleReturnStatus = data.sampleTransfer.sampleReturnStatus
              this.dataForm.sampleReturnAnomalyDescription = data.sampleTransfer.sampleReturnAnomalyDescription
              this.dataForm.sampleSupplier = data.sampleTransfer.sampleSupplier
              this.dataForm.returnSample = data.sampleTransfer.returnSample
              this.dataForm.returnTime = data.sampleTransfer.returnTime
              this.dataForm.sampleTesting = data.sampleTransfer.sampleTesting
              this.dataForm.startingTime = data.sampleTransfer.startingTime
              this.dataForm.startingTime1 = data.sampleTransfer.startingTime1
              this.dataForm.endTime1 = data.sampleTransfer.endTime1
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/reportapproval/sampletransfer/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'sampleNumber': this.dataForm.sampleNumber,
              'prototypeTrademark': this.dataForm.prototypeTrademark,
              'detectionSite': this.dataForm.detectionSite,
              'inspector': this.dataForm.inspector,
              'sampleCollectionSite': this.dataForm.sampleCollectionSite,
              'sampleCollectionPerson': this.dataForm.sampleCollectionPerson,
              'sampleReturnSite': this.dataForm.sampleReturnSite,
              'sampleReturnPerson': this.dataForm.sampleReturnPerson,
              'sampleLeading': this.dataForm.sampleLeading,
              'sampleAnomalyDescription': this.dataForm.sampleAnomalyDescription,
              'sampleReturnStatus': this.dataForm.sampleReturnStatus,
              'sampleReturnAnomalyDescription': this.dataForm.sampleReturnAnomalyDescription,
              'sampleSupplier': this.dataForm.sampleSupplier,
              'returnSample': this.dataForm.returnSample,
              'returnTime': this.dataForm.returnTime,
              'sampleTesting': this.dataForm.sampleTesting,
              'startingTime': this.dataForm.startingTime,
              'endTime': this.dataForm.endTime,
              'startingTime1': this.dataForm.startingTime1,
              'endTime1': this.dataForm.endTime1
            })
          }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
